using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace Core
{
    public delegate void UpdateWarningDelegate(string message);

    public class Utils
    {
        public static string DEFAULT_DATETIME_FORMAT_STRNG = "u";
        public static UpdateWarningDelegate UpdateWarningHandler = null;

        static public object Deserialize(string serializedObject, Type aType)
        {
            StringReader sr = new StringReader(serializedObject);
            XmlTextReader xmlTextReader = new XmlTextReader(sr);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlTextReader);

            XmlNodeReader reader = new XmlNodeReader(xmlDoc.DocumentElement);
            XmlSerializer serializer = new XmlSerializer(aType);
            object obj = serializer.Deserialize(reader);
            return obj;
        }

        static public object Deserialize(Stream inputStream, Type aType)
        {
            XmlSerializer serializer = new XmlSerializer(aType);

            return serializer.Deserialize(inputStream);
        }

        static public string Serialize(object anObject, Type aType)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, string.Empty);

            XmlSerializer serializer = new XmlSerializer(aType);
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            serializer.Serialize(sw, anObject, ns);
            String result = sb.ToString();
            result = result.Replace("encoding=\"utf-16\"", string.Empty);
            return result;
        }

        public static void OpenFileInExplorer(string path)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "explorer.exe";
            proc.StartInfo.Arguments = path;

            proc.Start();
        }

    }
}
